﻿using ICAM.controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ICAM.View
{
    /// <summary>
    /// LogInLoading.xaml 的交互逻辑
    /// </summary>
    public partial class LogInLoading : Window, INotifyPropertyChanged
    {
        private Window _parentWindow = null;
        public LogInLoading()
        {
            InitializeComponent();
            this.DataContext = this;
            this.Loaded += LogInLoading_Loaded;
        }

        public LogInLoading(Window wnd) : this()
        {
            _parentWindow = wnd;
        }

        private void LogInLoading_Loaded(object sender, RoutedEventArgs e)
        {
            Action action = new Action(StartLoading);
            action.BeginInvoke(LoadCompleteCallBack,null);
        }

        private void LoadCompleteCallBack(IAsyncResult asyncResult)
        {
            if (asyncResult.IsCompleted)
            {
                this.Dispatcher.Invoke(() =>
                {
                    this.Close();
                    ShowMainWindow();
                });

            }
        }

        /// <summary>
        /// 显示主页
        /// </summary>
        private void ShowMainWindow()
        {
            //AlaramInfoView alaramInfoView = new AlaramInfoView();
            MainWindow modernWindow = new MainWindow();
            Application.Current.MainWindow = modernWindow;
            modernWindow.Closed += ModernWindow_Closed;
            //modernWindow.Content = alaramInfoView;
            //modernWindow.WindowState = WindowState.Maximized;

            //double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            //double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            //double windowWidth = 1200;
            //double windowHeight = 800;

            //modernWindow.Left = (screenWidth / 2) - (windowWidth / 2);
            //modernWindow.Top = (screenHeight / 2) - (windowHeight / 2);
   

            modernWindow.Show();
            //modernWindow.Activate();
        }

        /// <summary>
        /// 主页关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModernWindow_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 加载百分比
        /// </summary>
        private int _Percent = 0;
        public int Percent
        {
            get { return this._Percent; }
            set {
                this._Percent = value;
                OnPropertyChanged("Percent");
            }
        }

        /// <summary>
        /// 加载函数
        /// </summary>
        public void StartLoading()
        {
            while (this._Percent < 100)
            {
                this.Percent += 1;
                Thread.Sleep(20);
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
